package cn.tedu.oa.sys.controller;

import cn.tedu.oa.common.annotation.LogAnnotation;
import cn.tedu.oa.common.util.ShiroUtils;
import cn.tedu.oa.common.vo.JsonResult;
import cn.tedu.oa.sys.entity.Department;
import cn.tedu.oa.sys.service.DepartmentService;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sys/department/")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /*跳转到部门首页*/
    @LogAnnotation("部门：访问首页")
    @RequestMapping("department_ui")
    public String doUserUI() {
        return "sys/department/department_ui";
    }
    
    /*跳转部门添加页面*/
    @LogAnnotation("部门：访问添加页面")
    @RequestMapping("department_add")
    public String doDeptAddUI() {
        return "sys/department/department_add";
    }
    
    /*跳转部门修改页面*/
    @LogAnnotation("部门：访问修改页面")
    @RequestMapping("department_edit")
    public String doDeptEditUI() {
        return "sys/department/department_edit";
    }

    /*获取所有部门信息,如果有参数id则查询除id之外的*/
    @LogAnnotation("部门：获取所有部门")
    @RequestMapping("getAll")
    @ResponseBody
    public JsonResult getExceptId(Integer id){
        List<Department> list = departmentService.getExceptId(id);
        JsonResult result  = new JsonResult(list);
        return result;
    }

    /*显示分页查询数据*/
    @LogAnnotation("部门：分页查询")
    @RequestMapping("get")
    @ResponseBody
    public JsonResult get(String criteria,Integer pageNo,Integer pageSize){
    	List<Department> list = departmentService.getDepartmentPagerList(criteria, pageNo, pageSize);
    	int rowCount = departmentService.getRowCount(criteria);
    	JsonResult result = new JsonResult();
    	result.setCount(rowCount);
    	result.setData(list);
    	return result;
    }

    /*通过id删除支持多个id*/
    @LogAnnotation("部门：删除功能")
    @RequestMapping("deleteByIds")
    @ResponseBody
    public JsonResult deleteByIds (Integer... id){
        String deleteUser = ShiroUtils.getPrincipal().getName();
    	departmentService.deletes(deleteUser,id);
    	return new JsonResult("删除成功");
    }

    /*添加部门操作*/
    @LogAnnotation("部门：添加功能")
    @RequestMapping("add")
    @ResponseBody
    public JsonResult add (Department dept){
        dept.setAddUser(ShiroUtils.getPrincipal().getName());
        dept.setAddTime(new Date());
        departmentService.add(dept);
        return new JsonResult("添加成功");
    }

    /*更新部门信息*/
    @LogAnnotation("部门：修改功能")
    @RequestMapping("update")
    @ResponseBody
    public JsonResult update (Department dept){
        dept.setUpdateUser(ShiroUtils.getPrincipal().getName());
        dept.setUpdateTime(new Date());
        departmentService.update(dept);
        return new JsonResult("修改成功");
    }

    /*获取传入的id下所有子部门*/
    @LogAnnotation("部门：获取子部门")
    @RequestMapping("getDivisions")
    @ResponseBody
    public JsonResult findDivisions (Integer... id){
        List<Department> list = departmentService.findDivisions(id);
        return new JsonResult(list);
    }
}
